<template>
  <basic-container>
    <avue-crud
        :data="data"
        :option="option"
        ref="curd"
        :page="page"
        v-model="form"
        :permission="permissionList"
        :table-loading="loading"
        :before-open="beforeOpen"
        @row-save="save"
        @row-del="del"
        @row-update="update"
        @search-change="search"
        @refresh-change="refresh"
        @selection-change="selectChange"
        @on-load="onLoad"
    >

      <template slot="menuLeft">
        <el-button
            type="primary"
            size="small"
            icon="el-icon-setting"
            @click="createCode"
        >生成代码
        </el-button>
        <el-button
            type="primary"
            size="small"
            icon="el-icon-download"
            v-if="permission.generate_excel"
            @click="exports"
        >导 出
        </el-button>
        <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            v-if="permission.generate_delete"
            @click="deletaAll"
        >删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
import {mapGetters} from "vuex";
import {create, del, exports, pageList, query, save, update} from "@/api/develop/generate";

export default {
  data() {
    return {
      loading: true,
      form: {},
      data: [],
      selectionList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        menuWidth: 300,
        height: 'auto',
        calcHeight: 100,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        selection: true,
        dialogClickModal: false,
        index: true,
        enter: true,
        copyBtn: true,
        viewBtn: true,
        printBtn: true,
        column: [
          {
            label: "表前缀",
            prop: "tablePrefix",
            rules: [
              {
                required: true,
                message: "请输入表前缀",
                trigger: "blur",
              },
            ],
          },
          {
            label: "表名",
            prop: "tableName",
            rules: [
              {
                required: true,
                message: "请输入表名",
                trigger: "blur",
              },
            ],
          },

          {
            label: "包名",
            prop: "parentPath",
            rules: [
              {
                required: true,
                message: "请输入包名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "模块名称",
            prop: "module",
            rules: [
              {
                required: true,
                message: "请输入模块名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "公共Entity",
            prop: "isParent",
            type: "radio",
            dicData: [
              {
                label: "继承",
                value: 0,
              },
              {
                label: "不继承",
                value: 1,
              },
            ],
            rules: [
              {
                required: true,
                message: "请输入继承公共entity",
                trigger: "blur",
              },
            ],
          },
          {
            label: "生成路径",
            prop: "javaFile",
            span: 24,
            rules: [
              {
                required: true,
                message: "生成路径",
                trigger: "blur",
              },
            ],
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        printBtn: this.vaildData(this.permission.generate_print, false),
        copyBtn: this.vaildData(this.permission.generate_copy, false),
        viewBtn: this.vaildData(this.permission.generate_view, false),
        addBtn: this.vaildData(this.permission.generate_add, false),
        delBtn: this.vaildData(this.permission.generate_delete, false),
        editBtn: this.vaildData(this.permission.generate_edit, false),
      };
    },
    delIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
  },
  methods: {
    createCode() {
      if (this.selectionList.length <= 0) {
        this.$message({
          type: "error",
          message: "请选择一条或多条数据",
        });
        return
      }
      create(this.delIds);
    },
    exports() {
      let url = exports()
      const a = document.createElement('a')
      a.href = url
      a.click();
    },
    onLoad(page, param) {
      this.loading = true;
      pageList(page.currentPage, page.pageSize, param).then((res) => {
        this.loading = false;
        const data = res.data.data;
        this.data = data.records;
        this.page.total = data.total;
      });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        query(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    save(row, loading, done) {
      loading();
      save(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
      done();
    },
    deletaAll() {
      if (this.selectionList.length <= 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return del(this.delIds);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return del(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
    },
    update(row, index, loading, done) {
      loading();
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
      done();
    },
    currentChange() {
    },
    refresh() {
      this.onLoad(this.page, "");
    },
    sizeChange() {
    },
    search(params, done) {
      this.onLoad(this.page, params);
      console.log(params);
      done();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
};
</script>
